/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {
  Form, Input, Radio, Select, Icon,
  Upload, Tooltip, DatePicker, Button, Typography,
} from 'antd';
import {connect} from 'dva';
import ReactDataGrid from 'react-data-grid';
import moment from 'moment'
import {download,renderReport} from '@/utils/utils';
import config from '@/config/config'
import find from 'lodash/find';
const { Text } = Typography;

const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const Option = Select.Option;

class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      users:[],
      parseData:[],
      columns:[]
    };
  }

  componentDidMount() {
    const that=this;
    let arr=[
      {name:'供水量',key:'gsl',unit:'万立方'},
      {name:'销水量',key:'xsl',unit:'万立方'},
      {name:'销售额',key:'xse',unit:'万 元'},
      {name:'排水收入',key:'pssr',unit:'万 元'},
      {name:'排水量',key:'psl',unit:'万立方'},
      {name:'校表收入',key:'xbsr',unit:'千 元'},
      {name:'维修',key:'wx',unit:'户'},
      {name:'维修收入',key:'wxsr',unit:'千 元'},
    ]

    let data=[]
    const items=JSON.parse(JSON.stringify(this.props.editRecord.items));
    console.log('items',items)
    const columns = [
      { key: "name", name: "",width:100,resizable: true,},
    ];
    for(let i=0;i<items.length;i++){
      columns.push(  { key: items[i].name, name: items[i].name,resizable: true,
        formatter: (event) => {
          return   <div  style={{
          }}>
            {event.value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          </div>;
        }},)
    }
    this.setState({
      columns
    },function() {
      for(let i=0;i<arr.length;i++){
        data[i]={name:arr[i].name,key:arr[i].key}
        for(let j=0;j<items.length;j++){
          let exit=find(items[j].items,function(o) {
            return o.key===arr[i].key
          })
          if(exit){
            data[i][`${items[j].name}`]=exit.value
          }
        }
      }

      console.log('data',data)
      this.setState({
        parseData:data
      })
    })
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 8},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 14},
      }
    };
    return (
      <div>
        <h1 style={{textAlign:'center'}}>产销旬报统计报告表</h1>
        <Form onSubmit={this.handleSubmit} style={{textAlign:'center'}}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               开始日期
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.start_date}</span>
          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               结束日期
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.end_date}</span>

          </FormItem>
        </Form>
        <ReactDataGrid
          columns={this.state.columns}
          rowGetter={i => this.state.parseData[i]}
          rowsCount={this.state.parseData.length}

          minHeight={315}
        />
        <Form style={{marginTop:'10px'}} onSubmit={this.handleSubmit}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               部门负责人
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.director}</span>

          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               制表
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.author}</span>

          </FormItem>
          {
            this.props.editRecord.original_files.length>0&&
            <FormItem
              style={{display:'inline-block',width:'33%'}}
              {...formItemLayoutWithLabel}
              label={'原文件'}
            >
              <div onClick={()=>{
                download(`${config.prefix}/reports/${this.props.editRecord.id}/original_files/${this.props.editRecord.original_files[0].key}/download??token=Bearer ${sessionStorage.getItem('token')}`)
              }} title={'点击下载'} style={{color:'blue',cursor:'pointer',textOverflow:'hidden',whiteSpace:'nowrap',overflow:'hidden'}}>
                {this.props.editRecord.original_files[0].name}
              </div>
            </FormItem>
          }

        </Form>
        <Text mark>使用方法 : </Text>
        <p>每10天1报，于每月11日、21日、31日报出，分送经理，副经理，总会计师，生产科存档。 </p>

      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


